package com.example.teachingSystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.teachingSystem.pojo.entity.Teacher;
import com.example.teachingSystem.pojo.form.BasePageQuery;
import com.example.teachingSystem.pojo.form.TS.AddTeachForm;
import com.example.teachingSystem.pojo.form.TS.UpdateTeachForm;
import com.example.teachingSystem.pojo.vo.TeachInfoVo;
import com.example.teachingSystem.service.TeacherService;
import com.example.teachingSystem.mapper.TeacherMapper;
import com.example.teachingSystem.utils.result.PageResult;
import com.example.teachingSystem.utils.result.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author 86147
* @description 针对表【teacher(教师表)】的数据库操作Service实现
* @createDate 2023-12-07 15:22:37
*/
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher>
    implements TeacherService{

    @Autowired
    private TeacherMapper teacherMapper;

    @Override
    public Result<String> adminAddTeacher(AddTeachForm addTeachForm) {
        Teacher teacher = new Teacher();
        BeanUtils.copyProperties(addTeachForm,teacher);
        Teacher teacher1 = teacherMapper.selectById(teacher);
        if(teacher1!=null){
            return Result.build(null,"添加失败，工号重复");
        }
        if(teacherMapper.insert(teacher)>0){
            return Result.ok("教师添加成功");
        }
        return Result.build(null,"教师添加失败");
    }

    @Override
    public Result<String> adminDeleteTeacher(Long teachId) {
        Teacher teacher = teacherMapper.selectById(teachId);
        if(teacher == null){
            return Result.build(null,"删除失败，查无此人");
        }
        if(teacherMapper.deleteById(teachId)>0){
            return Result.ok("教师删除成功");
        }
        return Result.build(null,"教师删除失败");
    }

    @Override
    public Result<TeachInfoVo> getTeacherById(Long teachId) {
        Teacher teacher = teacherMapper.selectById(teachId);
        if(teacher==null){
            return Result.build(null,"查无此人");
        }
        TeachInfoVo teachInfoVo = new TeachInfoVo();
        BeanUtils.copyProperties(teacher,teachInfoVo);
        return Result.ok(teachInfoVo);
    }

    @Override
    public PageResult<TeachInfoVo> adminGetTeacher(BasePageQuery basePageQuery) {
        Page<Teacher> page = new Page<>(basePageQuery.getPageNum(), basePageQuery.getPageSize());
        IPage<TeachInfoVo> iPage = teacherMapper.adminGetTeacher(page);
        iPage.setTotal(iPage.getRecords().size());
        return PageResult.success(iPage);
    }

    @Override
    public Result<String> adminUpadeTeacher(UpdateTeachForm updateTeachForm) {
        Teacher teacher = teacherMapper.selectOne(new LambdaQueryWrapper<Teacher>().
                eq(Teacher::getTeacherId,updateTeachForm.getTeacherId()));
        if(teacher==null){
            return Result.build(null,500,"不存在该老师");
        }
        BeanUtils.copyProperties(updateTeachForm,teacher);
        this.updateById(teacher);
        return Result.ok(null,"修改成功");
    }
}




